<template>
    <div class="login">
        <el-button-group class="btn-group">
            <el-button v-if="showDoctorLogin" type="primary" @click="doctorLogin" class="btn">医生登录</el-button>
            <el-button v-else type="" @click="doctorLogin" class="btn">医生登录</el-button>
            <el-button v-if="showAdminLogin" type="primary" @click="adminLogin" class="btn">管理员登录</el-button>
            <el-button v-else type="" @click="adminLogin" class="btn">管理员登录</el-button>
        </el-button-group>
        <el-card class="card">
            <template #header>
                <h3 v-if="showDoctorLogin">医生登录</h3>
                <h3 v-else>管理员登录</h3>
            </template>
            <el-form :rules="rules" ref="loginForm" :model="user">
                <el-form-item label="账号" prop="account">
                    <el-input v-model="user.account" />
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input v-model="user.password" type="password" show-password />
                </el-form-item>
            </el-form>
            <template #footer>
                <el-button type="primary" @click="login" class="btn-login">登录</el-button>
            </template>
        </el-card>
    </div>
</template>

<script>
import { ElMessage } from "element-plus";
import { LoginAPI } from "@/api/user";
export default {
    data() {
        return {
            user: {
                account: '',
                password: '',
                role: 1
            },
            rules: {
                account: { required: true, message: "请输入账号", trigger: "blur" },
                password: { required: true, message: "请输入密码", trigger: "blur" }
            },
            showDoctorLogin: true,
            showAdminLogin: false
        };
    },
    methods: {
        doctorLogin() {
            this.showDoctorLogin = true;
            this.showAdminLogin = false;
            this.user.role = 1;
        },
        adminLogin() {
            this.showAdminLogin = true;
            this.showDoctorLogin = false;
            this.user.role = 2;
        },
        login() {
            this.$refs.loginForm.validate((val) => {
                if (val) {
                    LoginAPI(this.user).then((resp) => {
                        if (resp.data.code == 2000) {
                            ElMessage.success(resp.data.message);
                            sessionStorage.setItem('user', JSON.stringify(resp.data.datas[0]));
                            sessionStorage.setItem('permission', JSON.stringify(resp.data.datas[1]));
                            this.$router.push('/');
                        } else {
                            ElMessage.error(resp.data.message);
                        }
                    })
                }
            });
        },
    },
};
</script>

<style scoped>
.login {
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
    width: 100%;
    height: 100%;
    background-image: url("@/assets/imgs/医生管理员登录背景图.jpg");
    background-repeat: no-repeat;
    background-size: cover;
    background-position: 0px, 0px;
}

.login .card {
    width: 400px;
    opacity: 0.90;
    border-radius: 10px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
}

.login .btn-group {
    width: 400px;
}

.login .btn-group .btn {
    width: 200px;
}

.login .btn-login {
    width: 300px;
}
</style>
